package com.sq.partner.manager.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.sq.partner.manager.common.exception.AppException;
import com.sq.partner.manager.dao.ITradProtocolValueDao;
import com.sq.partner.manager.mybatis.model.CoreDept;
import com.sq.partner.manager.mybatis.model.TradProtocolValue;
import com.sq.partner.manager.util.Page;

@Repository
public class TradProtocolValueDaoImpl extends BaseDaoImpl implements ITradProtocolValueDao{

	@SuppressWarnings("unchecked")
	@Override
	public Page<Map<String, Object>> queryTradProtocolValuePage(Page<Map<String, Object>> page, Map<String, Object> map) throws Exception {
		int count=(Integer) query("queryCountTradProtocolValuePage",map );
		page.setTotal(count);
		page.setData((List<Map<String, Object>>) queryList("queryListTradProtocolValuePage", map));
		return page;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> queryCJBTradProtocolValueByProtocolNo(String protocolNo) throws Exception {
		return (Map<String, Object>) query("queryCJBTradProtocolValueByProtocolNo", protocolNo);
	}

	@Override
	public String queryMaxSerialNumber(Map<String,Object> map) throws Exception {
		if("2".equals(map.get("planType"))){
			//财险最大流水号查询
			Object maxNo =  query("queryMaxSerialNumber",map);
			if(maxNo==null){
				return "00001";
			}else{
				String f = "%05d";
				return String.format(f, Integer.parseInt(maxNo.toString())+1);
			}
		}else{
			throw new AppException("方案类型不符合");
		}
	}

	@Override
	public void addTradProtocolValue(List<TradProtocolValue> lists,Map<String,Object> map) throws Exception {
		add("insertTradPlanProtocol",map);
		batchInsert("insertTradProtocolValue",lists);
	}

	@Override
	public void updateTradProtocolValue(List<TradProtocolValue> lists,Map<String,Object> map) throws Exception {
		String protocolNo = lists.get(0).getProtocolNo();
		int count = (Integer) query("queryCountGatewayPolicy", protocolNo);
		if(count>0) throw new AppException("该协议对应的产品已生成保单,不能修改协议信息！");
		delete("delTradPlanProtocol", protocolNo);
		delete("delTradProtocolValue", protocolNo);
		add("insertTradPlanProtocol",map);
		batchInsert("insertTradProtocolValue",lists);
	}

	@Override
	public void delTradProtocolValue(String protocolNo) throws Exception {
		int count = (Integer) query("queryCountGatewayPolicy", protocolNo);
		if(count>0) throw new AppException("该协议对应的产品已生成保单,不能删除协议信息！");
		delete("delTradPlanProtocol", protocolNo);
		delete("delTradProtocolValue", protocolNo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> queryProtocolExcel(Map<String, Object> map) throws Exception {
		return queryList("queryCJBProtocolExcel", map);
	}


}
